package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<Brand> brandPageQuery(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //设置PageHelper分页条件
        PageHelper.startPage(page, rows);
        //创建一个封装复杂条件查询的对象Example
        Example example = new Example(Brand.class);
        //封装查询条件
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(key)){
            criteria.orLike("name", "%"+key+"%");
            criteria.orEqualTo("letter", key.toUpperCase());
        }
        //封装排序条件   select * from xx where id=1 order by xx desc
        if(!StringUtils.isEmpty(sortBy)){
            //这里设置的是原始的sql语句
            example.setOrderByClause(sortBy+" "+(desc ? "DESC" : "ASC"));
        }
        //查询数据库
        List<Brand> brands = brandMapper.selectByExample(example);
        //判空
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        //得到一个PageHelper的分页对象
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);
        //封装自定义的分页对象
        PageResult<Brand> pageResult = new PageResult<>(
                pageInfo.getTotal(),
                pageInfo.getPages(),
                pageInfo.getList()
        );
        return pageResult;
    }

    public void saveBrand(Brand brand, List<Long> cids) {
        //用insertSelective，sql为：insert into tb_brand (name, image, letter) values (?, ?, ?)
        //用insert，sql为：insert into tb_brand (id, name, image, letter, create_time, update_time) values (?, ?, ?, ?, ?)
        try {
            //保存品牌对象
            brandMapper.insertSelective(brand);
            //维护中间表
            brandMapper.insertCategoryAndBrand(brand.getId(), cids);
        }catch (Exception e){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }

    }

    public Brand findBrandById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if(brand==null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brand;
    }

    public List<Brand> findBrandsByCid(Long id) {
        List<Brand> brands = brandMapper.findBrandsByCid(id);
        if(CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return brands;
    }

    public List<Brand> findBrandsByIds(List<Long> ids) {
        List<Brand> list = brandMapper.selectByIdList(ids);
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return list;
    }
}
